package services

import (
    "fmt"
    "volux/utils"
)

type LockService struct {
    etcd *utils.EtcdClient
}

func NewLockService(etcd *utils.EtcdClient) *LockService {
    return &LockService{etcd: etcd}
}

func (s *LockService) Lock(resource string, ttl int64) error {
    key := fmt.Sprintf("/locks/%s", resource)
    return s.etcd.PutWithTTL(key, "locked", ttl)
}

func (s *LockService) Unlock(resource string) error {
    key := fmt.Sprintf("/locks/%s", resource)
    return s.etcd.Delete(key)
}
